package eco2automotive.util;
import javax.script.*;
/**  
  * Utility for mathematical calcs.
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  *
  */ 
public class MathLib {
    /**
     * Utility
     * @param dec
     * @return
     */
    public String DecToHex(int dec){return Integer.toHexString(dec);}
/**
 * 
 * @param dec
 * @return
 */
public String DecToBin(int dec){return Integer.toBinaryString(dec);}
/**
 * 
 * @param hex
 * @return
 */
public int HexToDecimal(String hex){return Integer.parseInt(hex, 16);}
/**
 * 
 * @param hex
 * @return
 */
public String HexToBin(String hex){
    return Integer.toBinaryString(HexToDecimal(hex));
}
/**
 * 
 * @param expresion
 * @return
 */
public Object eval(String expresion){
  ScriptEngineManager manager = new ScriptEngineManager();    
  ScriptEngine engine = manager.getEngineByName("js"); 
  Object result=null;
  try{
   result = engine.eval(expresion);
  }
  catch(Exception e){}
    return result;
}

/**
 * 
 * @param data
 * @param masc
 * @param fx
 * @return
 */
public String calculatorXF(String data, String masc, String fx){
    System.out.println("FORMULA: "+fx);
    System.out.println("MASK: "+masc);
    System.out.println("DATA: "+data);
// Eg.  data=8f41f13a, masc=AABBCCDD, fx=A*256                    
    String str_to_eval=new UtilString().ClearTotalData(fx.toUpperCase());    
    //-----------------------------------------------------------------------    
    char dta[]=new UtilString().ClearTotalData(data.toUpperCase()).toCharArray();
    char msc[]=new UtilString().ClearTotalData(masc.toUpperCase()).toCharArray();
    char f[]=new UtilString().ClearTotalData(fx.toUpperCase()).toCharArray();
    //-----------------------------------------------------------------------
    for(int i=0; i<f.length;i++)
    {    
        if(Character.isLetter(f[i]))
        {
            System.out.println("Searching var.."+f[i]);
            boolean found=false;
            int z=0;
           while(found==false && z<msc.length){ 
               System.out.println("msc["+z+"]= "+msc[z]+" f["+i+"]= "+f[i]);                
                if(f[i]==msc[z])
   // AABB >> 1F81 -> f[3]==msc[0]==A and replacement the hexadecimal value in str_to_eval="A+245"
                        {                         
                            found=true;
                            String toDec=String.valueOf(dta[z]).concat(String.valueOf(dta[z+1]));                                                                                                                                                    
                            str_to_eval=str_to_eval.replace(String.valueOf(f[i]),String.valueOf(HexToDecimal(toDec)));                                                        
                    }
                ++z;                         
        }//while
        }                    
    }

try{
return (eval(str_to_eval).toString()); 
}catch(Exception e){return "<No Calc.> "+data;}
    
}

/**
 * This main method is a example.
 * @param args
 * @throws ScriptException
 */
public static void main(String[] args) throws ScriptException {
        MathLib m =new MathLib();       
        System.out.println(m.calculatorXF("1AF8","AABB","((A*256)+B)/4"));
    }

}   
